<?php

namespace app\curd\controller;

use app\admin\controller\Menu;
use think\admin\model\SystemMenu;
use think\admin\service\QueueService;
use think\helper\Str;

/**
 * 模块管理
 * Class Menu
 * @package app\curd\controller\config
 */
class MenuX extends Menu
{
    /**
     * 系统菜单管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '系统菜单管理增强';
        $this->type = $this->get['type'] ?? 'index';
        // 获取顶级菜单ID
        $this->pid = $this->get['pid'] ?? '';

        // 查询顶级菜单集合
        $this->menupList = SystemMenu::mk()->where(['pid' => 0, 'status' => 1])->order('sort desc,id asc')->column('id,pid,title', 'id');

        SystemMenu::mQuery()->layTable();
    }


    /**
     * 复制菜单
     * @auth true
     */
    public function copy()
    {
        $this->_applyFormToken();
        SystemMenu::mForm('form');
    }

    /**
     * 表单数据处理
     * @auth true
     */
    public function _copy_form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            unset($data['id']);
            unset($data['title']);
            unset($data['icon']);
        }
    }

    /**
     * 生成模块
     * @auth true
     */
    public function make()
    {
        $data = $this->_vali([
            'title.require' => '菜单名称不能为空',
            'node.require' => '权限节点不能为空',
        ]);
        [$app, $controller] = explode('/', $data['node']);
        [$app_s, $controller_s] = [Str::studly($app), Str::studly($controller)];//下划线转大驼峰
        //判断模块是否已经生成
//	    $this_class_path = $this->app->getBasePath() . $app . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . Str::studly($name) . '.php';
//	    if (is_file($this_class_path)) {
//		    $this->success('该模块模块已存在，请勿重复生成！');
//	    }
        //控制器
        QueueService::instance()->register('创建控制器', "xcurd:c {$app}@{$controller_s} {$app_s}{$controller_s}");
        // QueueService::instance()->register('创建API控制器', "xmake:new_api_controller dd6@api/{$module}/{$name_s} {$data['title']}");
        //模型
        QueueService::instance()->register('创建模型', "xcurd:m {$app}@{$app_s}{$controller_s}");
        // QueueService::instance()->register('创建日志模型', "xmake:new_model dd6@Dd6{$module_s}{$name_s}Log {$data['title']} log");
        // //服务
        // QueueService::instance()->register('创建服务', "xmake:new_service dd6@Dd6{$module_s}{$name_s}Service {$data['title']}");
        //视图
        QueueService::instance()->register('创建视图', "xcurd:v {$app}@{$controller_s}");
        //数据库
        QueueService::instance()->register('创建数据库', "xcurd:sql {$app} {$controller} {$data['title']}");
        $this->success('创建新模块任务成功！');
    }


}